package com.wuyou.simple;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 2341. 数组能形成多少数对
 */
public class NumberOfPairs {


    public static void main(String[] args) {
        int[] nums = {5, 12, 53, 22, 7, 59, 41, 54, 71, 24, 91, 74, 62, 47, 20, 14, 73, 11, 82, 2, 15, 38, 38, 20, 57, 70, 86, 93, 38, 75, 94, 19, 36, 40, 28, 6, 35, 86, 38, 94, 4, 90, 18, 87, 24, 22};

        System.out.println(sol_1(nums));
    }

    // 数组肯定比map快
    public static int[] sol_2(int[] nums) {
        int[] numCount = new int[101];
        int numPair = 0;
        for (int i = 0; i < nums.length; i++) {
            numCount[nums[i]] ++;
            if (numCount[nums[i]] % 2 == 0) {
                numPair ++;
            }
        }
        return new int[]{numPair, nums.length - numPair * 2};
    }

    // 1ms
    public static int[] sol_1(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        int[] res = {0, nums.length};
        for (int i = 0; i < nums.length; i++) {
            Integer numCount = map.getOrDefault(nums[i], 0) + 1;
            if (numCount > 0 && numCount % 2 == 0) {
                res[0]++;
                res[1] -= 2;
            }
            map.put(nums[i], numCount);
        }
        return res;
    }
}
